package com.enjoyor.soa.traffic.server.tsms.helper;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;
import com.enjoyor.soa.traffic.api.dto.tsms.RltRouteDeviceDto;
import com.enjoyor.soa.traffic.api.dto.tsms.RltRoutePostDto;
import com.enjoyor.soa.traffic.api.dto.tsms.RltTaskTimeDto;
import com.enjoyor.soa.traffic.api.dto.tsms.RouteInfoDto;
import com.enjoyor.soa.traffic.api.dto.tsms.RoutePointDto;
import com.enjoyor.soa.traffic.api.dto.tsms.SchemeCheckDto;
import com.enjoyor.soa.traffic.api.dto.tsms.SchemeFileDto;
import com.enjoyor.soa.traffic.api.dto.tsms.SchemeInfoDto;
import com.enjoyor.soa.traffic.api.dto.tsms.TaskDetailInfoDto;
import com.enjoyor.soa.traffic.api.dto.tsms.TaskInfoDto;
import com.enjoyor.soa.traffic.core.tsms.domain.RltRouteDevice;
import com.enjoyor.soa.traffic.core.tsms.domain.RltRoutePost;
import com.enjoyor.soa.traffic.core.tsms.domain.RltTaskTime;
import com.enjoyor.soa.traffic.core.tsms.domain.RouteInfo;
import com.enjoyor.soa.traffic.core.tsms.domain.RoutePoint;
import com.enjoyor.soa.traffic.core.tsms.domain.SchemeCheck;
import com.enjoyor.soa.traffic.core.tsms.domain.SchemeFile;
import com.enjoyor.soa.traffic.core.tsms.domain.SchemeInfo;
import com.enjoyor.soa.traffic.core.tsms.domain.TaskDetailInfo;
import com.enjoyor.soa.traffic.core.tsms.domain.TaskInfo;
import com.enjoyor.soa.traffic.util.helper.ReflectHelper;
/**
 * 实体类转换
 * @author fzj
 * @date 2018-08-31
 */
public class EntityChangeHelper {
	
	private static final Logger logger = Logger.getLogger(EntityChangeHelper.class);

	/**
	 * 将方案信息Pojo类转换成Dto类
	 * 如果内部存在List，一起转换
	 * @param list
	 * @return
	 */
	public static List<SchemeInfoDto> schemePojoToDtoList(List<SchemeInfo> listPojo) {
		List<SchemeInfoDto> listDto = new ArrayList<SchemeInfoDto>();
		try {
			if(listPojo == null || listPojo.size() <= 0) {
				return listDto;
			}
			for (SchemeInfo schemeInfo : listPojo) {
				listDto.add(schemePojoToDto(schemeInfo));
			}
		}catch(Exception ex){
			logger.info(String.format("schemePojoToDtoList error: ",new Object[] { JSON.toJSONString(ex.getMessage()) }));
			return listDto;
		}
		return listDto;
	}
	
	public static SchemeInfoDto schemePojoToDto(SchemeInfo pojo) {
		SchemeInfoDto dto = new SchemeInfoDto();
		try {
			if(pojo == null) {
				return dto;
			}
			dto = ReflectHelper.beanToBean(pojo, SchemeInfoDto.class);
			List<SchemeFile> schemeFileList = pojo.getSchemeFileList();
			if(null != schemeFileList && schemeFileList.size() > 0) {
				dto.setSchemeFileList(ReflectHelper.listToList(schemeFileList, SchemeFileDto.class));
			}
			List<SchemeCheck> schemeCheckList = pojo.getSchemeCheckList();
			if(null != schemeCheckList && schemeCheckList.size() > 0) {
				dto.setSchemeCheckList(ReflectHelper.listToList(schemeCheckList, SchemeCheckDto.class));
			}
		}catch(Exception ex){
			logger.info(String.format("schemePojoToDto error: ",new Object[] { JSON.toJSONString(ex.getMessage()) }));
			return dto;
		}
		return dto;
	}
	/**
	 * 将任务信息Dto类转换成Pojo类
	 * 如果内部存在List，一起转换
	 * @param list
	 * @return
	 */
	public static List<SchemeInfo> schemeDtoToPojoList(List<SchemeInfoDto> listDto) {
		List<SchemeInfo> listPojo = new ArrayList<SchemeInfo>();
		try {
			if(listDto == null || listDto.size() <= 0) {
				return listPojo;
			}
			for (SchemeInfoDto schemeInfoDto : listDto) {
				listPojo.add(schemeDtoToPojo(schemeInfoDto));
			}
		}catch(Exception ex){
			logger.info(String.format("schemeDtoToPojoList error: ",new Object[] { JSON.toJSONString(ex.getMessage()) }));
			return listPojo;
		}
		return listPojo;
	}
	public static SchemeInfo schemeDtoToPojo(SchemeInfoDto dto){
		SchemeInfo pojo = new SchemeInfo();
		try {
			if(dto == null) {
				return pojo;
			}
			pojo = ReflectHelper.beanToBean(dto, SchemeInfo.class);
			List<SchemeFileDto> schemeFileDtoList = dto.getSchemeFileList();
			if(null != schemeFileDtoList && schemeFileDtoList.size() > 0) {
				pojo.setSchemeFileList(ReflectHelper.listToList(schemeFileDtoList, SchemeFile.class));
			}
			
			List<SchemeCheckDto> schemeCheckDtoList = dto.getSchemeCheckList();
			if(null != schemeCheckDtoList && schemeCheckDtoList.size() > 0) {
				pojo.setSchemeCheckList(ReflectHelper.listToList(schemeCheckDtoList, SchemeCheck.class));
			}
		} catch(Exception ex) {
			logger.info(String.format("schemeDtoToPojo error: ",new Object[] { JSON.toJSONString(ex.getMessage()) }));
			return pojo;
		}
		return pojo;
	}
	
	/**
	 * 将任务信息Pojo类转换成Dto类
	 * 如果内部存在List，一起转换
	 * @param list
	 * @return
	 */
	public static List<TaskInfoDto> taskPojoToDtoList(List<TaskInfo> listPojo) {
		List<TaskInfoDto> listDto = new ArrayList<TaskInfoDto>();
		try {
			if(listPojo == null || listPojo.size() <= 0) {
				return listDto;
			}
				
			for (TaskInfo taskInfo : listPojo) {
				listDto.add(taskPojoToDto(taskInfo));
			}
		}catch(Exception ex){
			logger.info(String.format("taskPojoToDtoList error: ",new Object[] { JSON.toJSONString(ex.getMessage()) }));
			return listDto;
		}
		return listDto;
	}
	
	public static TaskInfoDto taskPojoToDto(TaskInfo pojo) {
		TaskInfoDto dto = new TaskInfoDto();
		try {
			if(pojo == null) {
				return dto;
			}
				
			dto = ReflectHelper.beanToBean(pojo, TaskInfoDto.class);
			List<RltTaskTime> rltTimeList = pojo.getRltTimeList();
			if(null != rltTimeList && rltTimeList.size() > 0) {
				dto.setRltTimeList(ReflectHelper.listToList(rltTimeList, RltTaskTimeDto.class));
			}
		}catch(Exception ex){
			logger.info(String.format("taskPojoToDto error: ",new Object[] { JSON.toJSONString(ex.getMessage()) }));
			return dto;
		}
		return dto;
	}
	/**
	 * 将任务信息Dto类转换成Pojo类
	 * 如果内部存在List，一起转换
	 * @param list
	 * @return
	 */
	public static List<TaskInfo> taskDtoToPojoList(List<TaskInfoDto> listDto) {
		List<TaskInfo> listPojo = new ArrayList<TaskInfo>();
		try {
			if(listDto == null || listDto.size() <= 0) {
				return listPojo;
			}
			for (TaskInfoDto taskInfoDto : listDto) {
				listPojo.add(taskDtoToPojo(taskInfoDto));
			}
		}catch(Exception ex){
			logger.info(String.format("taskDtoToPojoList error: ",new Object[] { JSON.toJSONString(ex.getMessage()) }));
			return listPojo;
		}
		return listPojo;
	}
	public static TaskInfo taskDtoToPojo(TaskInfoDto dto){
		TaskInfo pojo = new TaskInfo();
		try {
			if(dto == null) {
				return pojo;
			}
			pojo = ReflectHelper.beanToBean(dto, TaskInfo.class);
			List<RltTaskTimeDto> rltTimeDtoList = dto.getRltTimeList();
			if(null != rltTimeDtoList && rltTimeDtoList.size() > 0) {
				pojo.setRltTimeList(ReflectHelper.listToList(rltTimeDtoList, RltTaskTime.class));
			}
		} catch(Exception ex) {
			//LoggerHelper.LOG.error("getEventInfoPojo error: " + ex.getMessage());
			logger.info(String.format("taskDtoToPojo error: ",new Object[] { JSON.toJSONString(ex.getMessage()) }));
			return pojo;
		}
		return pojo;
	}
	
	/**
	 * 将执行任务信息Pojo类转换成Dto类
	 * 如果内部存在List，一起转换
	 * @param list
	 * @return
	 */
	public static List<TaskDetailInfoDto> taskDetailPojoToDtoList(List<TaskDetailInfo> listPojo) {
		List<TaskDetailInfoDto> listDto = new ArrayList<TaskDetailInfoDto>();
		try {
			if(listPojo == null || listPojo.size() <= 0) {
				return listDto;
			}
				
			for (TaskDetailInfo taskDetailInfo : listPojo) {
				listDto.add(taskDetailPojoToDto(taskDetailInfo));
			}
		}catch(Exception ex){
			logger.info(String.format("taskDetailPojoToDtoList error: ",new Object[] { JSON.toJSONString(ex.getMessage()) }));
			return listDto;
		}
		return listDto;
	}
	
	public static TaskDetailInfoDto taskDetailPojoToDto(TaskDetailInfo pojo) {
		TaskDetailInfoDto dto = new TaskDetailInfoDto();
		try {
			if(pojo == null) {
				return dto;
			}
				
			dto = ReflectHelper.beanToBean(pojo, TaskDetailInfoDto.class);
			List<RltTaskTime> rltTimeList = pojo.getRltTimeList();
			if(null != rltTimeList && rltTimeList.size() > 0) {
				dto.setRltTimeList(ReflectHelper.listToList(rltTimeList, RltTaskTimeDto.class));
			}
			
			List<RouteInfo> routeInfoList = pojo.getRouteInfoList();
			if(null != routeInfoList && routeInfoList.size() > 0) {
				dto.setRouteInfoList(ReflectHelper.listToList(routeInfoList, RouteInfoDto.class));
			}
		}catch(Exception ex){
			logger.info(String.format("taskDetailPojoToDto error: ",new Object[] { JSON.toJSONString(ex.getMessage()) }));
			return dto;
		}
		return dto;
	}
	/**
	 * 将执行任务信息Dto类转换成Pojo类
	 * 如果内部存在List，一起转换
	 * @param list
	 * @return
	 */
	public static List<TaskDetailInfo> taskDetailDtoToPojoList(List<TaskDetailInfoDto> listDto) {
		List<TaskDetailInfo> listPojo = new ArrayList<TaskDetailInfo>();
		try {
			if(listDto == null || listDto.size() <= 0) {
				return listPojo;
			}
			for (TaskDetailInfoDto taskDetailInfoDto : listDto) {
				listPojo.add(taskDetailDtoToPojo(taskDetailInfoDto));
			}
		}catch(Exception ex){
			logger.info(String.format("taskDetailDtoToPojoList error: ",new Object[] { JSON.toJSONString(ex.getMessage()) }));
			return listPojo;
		}
		return listPojo;
	}
	public static TaskDetailInfo taskDetailDtoToPojo(TaskDetailInfoDto dto){
		TaskDetailInfo pojo = new TaskDetailInfo();
		try {
			if(dto == null) {
				return pojo;
			}
			pojo = ReflectHelper.beanToBean(dto, TaskDetailInfo.class);
			List<RltTaskTimeDto> rltTimeDtoList = dto.getRltTimeList();
			if(null != rltTimeDtoList && rltTimeDtoList.size() > 0) {
				pojo.setRltTimeList(ReflectHelper.listToList(rltTimeDtoList, RltTaskTime.class));
			}
			List<RouteInfoDto> routeInfoDtoList = dto.getRouteInfoList();
			if(null != routeInfoDtoList && routeInfoDtoList.size() > 0) {
				pojo.setRouteInfoList(ReflectHelper.listToList(routeInfoDtoList, RouteInfo.class));
			}
		} catch(Exception ex) {
			logger.info(String.format("taskDetailDtoToPojo error: ",new Object[] { JSON.toJSONString(ex.getMessage()) }));
			return pojo;
		}
		return pojo;
	}
	
	public static List<RouteInfo> routeInfoDtoToPojoList(List<RouteInfoDto> listDto) {
		List<RouteInfo> listPojo = new ArrayList<RouteInfo>();
		try {
			if(listDto == null || listDto.size() <= 0) {
				return listPojo;
			}
			for (RouteInfoDto taskDetailInfoDto : listDto) {
				listPojo.add(routeInfoDtoToPojo(taskDetailInfoDto));
			}
		}catch(Exception ex){
			logger.info(String.format("taskDetailDtoToPojoList error: ",new Object[] { JSON.toJSONString(ex.getMessage()) }));
			return listPojo;
		}
		return listPojo;
	}
	
	public static RouteInfo routeInfoDtoToPojo(RouteInfoDto dto){
		RouteInfo pojo = new RouteInfo();
		try {
			if(dto == null) {
				return pojo;
			}
			pojo = ReflectHelper.beanToBean(dto, RouteInfo.class);
			List<RltRouteDeviceDto> rltTimeDtoList = dto.getRltRouteDeviceList();
			if(null != rltTimeDtoList && rltTimeDtoList.size() > 0) {
				pojo.setRltRouteDeviceList(ReflectHelper.listToList(rltTimeDtoList, RltRouteDevice.class));
			}
			
			
			List<RltRoutePostDto> rlRltRoutePostList=dto.getRltRoutePostList();
			if(null != rlRltRoutePostList && rlRltRoutePostList.size() > 0) {
				pojo.setRltRoutePostList(ReflectHelper.listToList(rlRltRoutePostList, RltRoutePost.class));
			}
						
			List<RoutePointDto> routePointList = dto.getRoutePointList();
			if(null != routePointList && routePointList.size() > 0) {
				pojo.setRoutePointList(ReflectHelper.listToList(routePointList, RoutePoint.class));
			}

		} catch(Exception ex) {
			logger.info(String.format("taskDetailDtoToPojo error: ",new Object[] { JSON.toJSONString(ex.getMessage()) }));
			return pojo;
		}
		return pojo;
	}
}
